package com.lams.service.system.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lams.jdbc.IBaseDAO;
import com.lams.jdbc.QueryUtils;
import com.lams.service.BaseFacade;
import com.lams.service.system.IRoleFacade;
import com.lams.util.ResultEntity;
import com.lams.util.SqlBean;

public class RoleFacade extends BaseFacade implements IRoleFacade {
	private String queryAllRole;
	private String findRidByUid;
	private String deleteFnbyRid;
	private String insertRf;
	@Override
	public ResultEntity operateRole(int start,int limit) {
		String sql = QueryUtils.getSqlBySqlCode(queryAllRole, null);
		int totalCount = QueryUtils.getTotalCount(sql);
		List<Map> data = QueryUtils.executeQuery(start, limit, sql);
		ResultEntity result = new ResultEntity(data,totalCount);
		result.setSuccess(true);
		return result;
	}
	@Override
	public List<Integer> gerRoleIdByUserId(String userId) {
		Map params = new HashMap();
		params.put("USER_ID", userId);
		String sql = QueryUtils.getSqlBySqlCode(findRidByUid, params);
		List<Map> data = QueryUtils.executeQuery(sql);
		List<Integer> result = new ArrayList<Integer>();
		for(Map item:data){
			result.add(Integer.parseInt((String)item.get("ROLE_ID")));
		}
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public ResultEntity changeRf(String rid, String[] fids) {
		//删除角色拥有权限
		Map params = new HashMap();
		params.put("ROLE_ID", rid);
		ResultEntity result = new ResultEntity();
		String delSql = QueryUtils.getSqlBySqlCode(deleteFnbyRid, params);
		try {
			super.getDao().executeUpdate(delSql);
		} catch (Exception e1) {
			logger.error(e1);
			result.setSuccess(false);
			result.setMessage("角色权限分配失败");
			return result;
		}
		//插入角色新的权限
		List<Map> source = new ArrayList<Map>();
		if(fids.length == 0){
			result.setSuccess(true);
			result.setMessage("角色权限分配成功");
			return result;
		}
		for(String fid:fids){
			Map item = new HashMap();
			item.put("ROLE_ID", rid);
			item.put("FUN_ID", fid);
			source.add(item);
		}
		SqlBean map = QueryUtils.getSqlBySqlCode(insertRf);
		if(map==null){
			result.setSuccess(false);
			result.setMessage("SQL编码:["+insertRf+"]未找到");
			return result;
		}
		String sql = map.getContent();
		try {
			int[] flag = super.getDao().batchUpdate(sql, source);
			result.setSuccess(true);
			result.setMessage("角色权限分配成功");
		} catch (SQLException e) {
			logger.error(e.getMessage());
//			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("角色权限分配失败");
			return result;
		}
		return result;
	}

	public String getQueryAllRole() {
		return queryAllRole;
	}

	public void setQueryAllRole(String queryAllRole) {
		this.queryAllRole = queryAllRole;
	}
	public String getFindRidByUid() {
		return findRidByUid;
	}
	public void setFindRidByUid(String findRidByUid) {
		this.findRidByUid = findRidByUid;
	}
	public String getDeleteFnbyRid() {
		return deleteFnbyRid;
	}
	public void setDeleteFnbyRid(String deleteFnbyRid) {
		this.deleteFnbyRid = deleteFnbyRid;
	}
	public String getInsertRf() {
		return insertRf;
	}
	public void setInsertRf(String insertRf) {
		this.insertRf = insertRf;
	}

}
